﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.Core
{
    public partial class CoreService : ICoreService
    {
        #region privilege

        public bool SavePrivilege(Privilege privilege)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                EntityRepository.Save(privilege, context);

                return EntityRepository.Commit(context);
            }
        }

        public bool DeletePrivilege(Guid id)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                EntityRepository.Delete<Privilege>(id, context);
                return EntityRepository.Commit(context);
            }
        }

        public Privilege GetPrivilege(Guid id)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return EntityRepository.Get(id, context) as Privilege;
            }
        }

        public PagedList<Privilege> GetPrivileges(string codeFilter, int start, int limit)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                List<Privilege> list = null;
                if (string.IsNullOrEmpty(codeFilter))
                {
                    list = context.Set<Privilege>()
                        .OrderBy(p => p.Code)
                        .Take(limit)
                        .Skip(start)
                        .ToList();

                    int totalCount = context.Set<Privilege>().Count();

                    PagedList<Privilege> pagedList = new PagedList<Privilege>();
                    pagedList.AddRange(list);
                    pagedList.Start = start;
                    pagedList.Limit = limit;
                    pagedList.Total = totalCount;
                    return pagedList;
                }
                else
                {
                    list = context.Set<Privilege>().Where(p => p.Code.Contains(codeFilter))
                        .OrderBy(p => p.Code)
                        .Take(limit)
                        .Skip(start)
                        .ToList();

                    int totalCount = context.Set<Privilege>().Where(p => p.Code.Contains(codeFilter)).Count();

                    PagedList<Privilege> pagedList = new PagedList<Privilege>();
                    pagedList.AddRange(list);
                    pagedList.Start = start;
                    pagedList.Limit = limit;
                    pagedList.Total = totalCount;
                    return pagedList;
                }
            }
        }

        public PagedList<Privilege> FindPrivileges(string code, int start, int limit)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                List<Privilege> list = context.Set<Privilege>().Where(p => p.Code.Contains(code))
                    .OrderBy(p => p.Code)
                    .Take(limit)
                    .Skip(start)
                    .ToList();

                int totalCount = context.Set<Privilege>().Where(p => p.Code.Contains(code)).Count();

                PagedList<Privilege> pagedList = new PagedList<Privilege>();
                pagedList.AddRange(list);
                pagedList.Start = start;
                pagedList.Limit = limit;
                pagedList.Total = totalCount;
                return pagedList;
            }
        }

        public ServiceResult AssignPrivilege(Guid privilegeId, Guid ownerId, PrivilegeOwnerType type)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                if (context.Set<AssignedPrivilege>().Where(p => p.PrivilegeId == privilegeId && p.OwnerId == ownerId && p.OwnerType == type.Code).Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    AssignedPrivilege ap = new AssignedPrivilege();
                    ap.Id = Guid.NewGuid();
                    ap.PrivilegeId = privilegeId;
                    ap.OwnerId = ownerId;
                    ap.OwnerType = type.Code;
                    context.Set<AssignedPrivilege>().Add(ap);
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true);
                }
                return new ServiceResult(false);
            }
        }

        public ListResult<Privilege> GetPrivileges(Guid ownerId, PrivilegeOwnerType type)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return new ListResult<Privilege>(context.Set<Privilege>()
                    .Join(context.Set<AssignedPrivilege>().Where(p => p.OwnerId == ownerId && p.OwnerType == type.Code),
                    l => l.Id,
                    r => r.PrivilegeId,
                    (l, r) => l).ToList());
            }
        }
        #endregion
    }
}
